import api from '../http/index.js'
import expireStorage from '@/util/storeUtil.js'

// 上次启动时的用户信息
let userInfoHistory = uni.getStorageSync('userInfo') || {};
const user = {

	state: {
		//用户信息
		info: userInfoHistory
	},
	
	mutations: {
		Set_Login_Info(state, data) {
			let _info = state.info;
			state.info = Object.assign({}, _info, data);
			//存储最新的用户数据到本地持久化存储
			uni.setStorageSync('userInfo', data);
		},
		Set_Some_Login_Info(state, data) {
			if(data){
				let _info = state.info;
				_info[data.column] = data.data
				console.log('修改',_info);
				//存储最新的用户数据到本地持久化存储
				uni.setStorageSync('userInfo', _info);
			}
		},
		Set_Logout(state) {
			state.info = {};
			uni.setStorageSync('userInfo', {});
		}
	},
	
	actions: {

		getUserInfo({commit, state}) {
			//登录成功后的操作
			api.getUserInfo().then(({ data }) => {
				if (data.code == "000000") {
					commit('Set_Login_Info',data.data)
				}
			})
		},
		// 登出
		logout({ commit, state }) {
			commit('Set_Logout')
			if (expireStorage.getCache('accessToken')) {
				return api.logout().then(({ data }) => {
						if (data.code == "000000") {
							commit('REMOVE_ACCESS_TOKEN')
							commit('REMOVE_REFRESH_TOKEN')
							return true
						}else{
							return false
						}
					})	
			}
			return false
		},
	}
}
export default user
